/*
 * @Author: tansq
 * @Date: 2021-07-22 10:17:05
 * @LastEditTime: 2021-08-25 17:11:51
 * @LastEditors: tansq
 * @Description: 抽取公共函数用作粘贴板内容的复制粘贴
 * @FilePath: \hte-vue-dev\src\utils\pasteMixin.js
 * @可以输入预定的版权声明、个性签名、空行等
 */


var excelData;
var arrMaxLength = 1000;

function setArrMaxLength(arrayLength) {
    if (!!arrayLength && typeof arrayLength === 'number' && arrayLength > 0) {
        arrMaxLength = arrayLength;
    }
}


export const pasteMixin = {
    data(){
      return {
        
        pastExcelrows:[],
        importExcelData:[],
        excelDataSource:[],

        excelLocale:{
            emptyText:'请从excel复制行数据到此处粘贴(ctrl+v)'
        },
       

      }
    
    },
  
    methods:{
        //粘贴事件
        pasteExcel: function(e){
            let _that = this
            
            
            this.pastExcelrows = [];
            this.importExcelData = [];
            var clipboardData = e.clipboardData; // IE
            if (!clipboardData) { // chrome
                clipboardData = e.originalEvent.clipboardData
            }
            if (!(clipboardData && clipboardData.items)) {
                this.$message.error("浏览器不支持！")
               
                return;
            }
            var excelData = clipboardData.getData("Text");
            var eData = getExcelData(e);  
            var rowStrArray = excelData.split("\n");
            if (rowStrArray.length > arrMaxLength) {
                this.$message.error("导入数量控制在"+ arrMaxLength + "以下！"  )
              
                return;
            }
            var rows = [];
            for (var i = 0; i < rowStrArray.length; i++) {
              var row = [];
              if (!rowStrArray[i] || rowStrArray[i].trim().length === 0) {
                    continue;
                }
              var tdStrArray = rowStrArray[i].split("\t");
              for (var j = 0; j < tdStrArray.length; j++) {
                row.push(tdStrArray[j]);
              }
             
            
              rows.push(row);
            }
   
            // 如果没有这个方法就报错
            try {
                if (rows instanceof Array && rows.length > 0) {          
                    this.pastExcelrows = rows;
                    
                        for (let index = 0; index < rows.length; index++) {
                            const excelItem = rows[index];
                            if(!!this.columnsExcel) {
                                let rowObj = {}
                                var realColumns = realColumnsExcel(this.columnsExcel);
                                realColumns.forEach((element,columnsIndex )=> {
                                    //导入列字段和表格顺序要一致
                                    //列字段数字数字、时间类型处理
                                      //默认字符串
                                      console.info(element)
                                      if(element.affix){
                                        rowObj[element.dataIndex] = element.affixValue
                                      }else if(element.isImportExceStringType == false){
                                        rowObj[element.dataIndex] =  !!excelItem[columnsIndex]?excelItem[columnsIndex].trim(): null
                                     }else{
                                        rowObj[element.dataIndex] =  !!excelItem[columnsIndex]?excelItem[columnsIndex].trim(): ''            
                                     }     
                                });   
                                rowObj.key=index;
                                _that.importExcelData.push(rowObj); 
                                   
                            }
                        }
                        this.excelDataSource =  _that.importExcelData;       
                             
                    
                } else {
                    this.$message.error("粘贴数据出错"+eData);
                    
                }
            } catch (e) {
                this.$message.error("粘贴数据出错");
                console.log(e);
            } finally{
                
            }
        },
        showModalExcel:function(){
            this.excelVisible = true;
        },   
        handleOkExcel:function(){
            if(this.excelDataSource.length == 0){
                this.$message.error("请导入数据！");
                return;
            }
            this.confirmLoading = true;
            this.$emit('handleOkExcel',this.excelDataSource);  
           
        },
        handleCancelExcel:function(){
            // this.excelVisible = false;
            
            this.$emit('handleCancelExcel');
        }
    }
}
/**
 * 最终列数据
 *  */  
function realColumnsExcel(columnsExcel){
    var realColumns = [];
    for (let index = 0; index < columnsExcel.length; index++) {
        const element = columnsExcel[index];
        if(element.isImportExcelColumns == false){
            continue      
        }   
        realColumns.push(element);
    }
    
    return realColumns;
}
/**
 *
 * @returns 1000行以内的二维数组
 */
function getExcelData(e) {
    var isIE = !compatibleBrowser();
    if (isIE) {
        excelData = e.clipboardData.getData('text');
    }

    if (excelData) {
        var newArr = [];

        if (typeof excelData === "string") {
            newArr = excelData.split("\n");
            if (newArr.length < 0) {
                return "复制内容空白！";
            }

            for (i = 0; i < newArr.length; i++) {
                var item = newArr[i];
                newArr[i] = item.replace(/\"/g, "").replace(/(\s*$)/g, "");
            }
            excelData = newArr.slice(0, newArr.length - 1);
        }

        if (excelData instanceof Array) {
            if (excelData.length > arrMaxLength) {
                var newArr = excelData.slice(0, arrMaxLength);
                excelData = newArr;
            }
        }

        return excelData;
    } else {
        if (isIE) {
            // this.$message.error("没有获取到内容！");
            return "没有获取到内容！";
        } else {
            // this.$message.error("非IE浏览器请先按键盘“Ctrl + V");
            return "非IE浏览器请先按键盘“Ctrl + V”";
        }
    }
}

function getFirstColumnArray(arr) {
    var newArr = [];
    if (arr) {
        newArr = new Array(arr.length);
        for (i = 0; i < arr.length; i++) {
            var itemArr = arr[i].split(/\s+/);
            if (itemArr != null && itemArr.length > 0) {
                newArr[i] = itemArr[0];
            } else {
                newArr[i] = "";
            }
        }
    }
    return newArr;
}

function compatibleBrowser() {
    var userAgent = window.navigator.userAgent;
    if (userAgent.indexOf("Chrome") > -1 || userAgent.indexOf("Firefox") > -1
        || userAgent.indexOf("Opera") > -1) {
        return true;
    } else {
        return false;
    }
}
